package com.hkemmm.controller;

import com.hkemmm.model.UserModel;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

//@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("执行了后台的Servlet业务逻辑");

        //重定向
        //重定向时浏览器向服务器发送了两次请求，不可以利用request携带数据
        //response.sendRedirect("views/logined.jsp");

        //转发
        //转发时浏览器只向服务器发送一次请求，可以使用同一个request携带数据(request的作用域为一次请求)
        //request.getRequestDispatcher("views/logined.jsp").forward(request,response);


        String userName = request.getParameter("userName");
        String userPsw = request.getParameter("userPsw");

        request.setAttribute("userName",userName);
        UserModel user = new UserModel();
        if(user.canLogin(userName,userPsw)){
            response.sendRedirect("views/logined.jsp");
        }else {
            request.getRequestDispatcher("WEB-INF/viewpages/logined.jsp").forward(request,response);
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
